//
//  WTChatRoomManagerHandle.m
//  WTLiveSDKDemo
//
//  Created by ben on 16/9/27.
//  Copyright © 2016年 ben. All rights reserved.
//

#import "WTChatRoomManagerHandle.h"

@interface WTChatRoomManagerHandle ()<WTChatRoomManager>
@property (nonatomic,strong) NIMSession*    chatRoomSession;
@end


@implementation WTChatRoomManagerHandle
@synthesize chat_room_connect_status;

#pragma mark private methods
- (void)sendEnterChatRoomMessage{
    
    NIMMessage* message = [[NIMMessage alloc] init];
    message.remoteExt = [WTMessageFactory chatRoomEnterRemoteExt];
    [[NIMSDK sharedSDK].chatManager sendMessage:message toSession:self.chatRoomSession error:nil];
}

- (void)sendExitChatRoomMessage{
    
    NIMMessage* message = [[NIMMessage alloc] init];
    message.remoteExt = [WTMessageFactory chatRoomExitRemoteExt];
    [[NIMSDK sharedSDK].chatManager sendMessage:message toSession:self.chatRoomSession error:nil];
}


#pragma mark  WTChatRoomManager
- (void)setRoomID:(NSString *)roomID{
    
    if(self.chatRoomSession && [self.chatRoomSession.sessionId isEqualToString:roomID]){
        return;
    }
    self.chatRoomSession = [NIMSession session:roomID type:NIMSessionTypeChatroom];
}

- (void)enterChatroom:(WTChatRoomManagerBlock)completion{
    
    
    NIMChatroomEnterRequest *request = [[NIMChatroomEnterRequest alloc] init];
    request.roomId = self.chatRoomSession.sessionId;
    request.roomExt = [[WTMessageFactory enterChatRoomRemoteExt] mj_JSONString];
    __weak __typeof(self)weakSelf = self;
    [[NIMSDK sharedSDK].chatroomManager enterChatroom:request completion:^(NSError * _Nullable error, NIMChatroom * _Nullable chatroom, NIMChatroomMember * _Nullable me) {
        
        if(!error){ //进入聊天室成功
            completion(YES,1);
            [weakSelf sendEnterChatRoomMessage];
            weakSelf.chat_room_connect_status = 1;
        }else {
            completion(YES,0);
            weakSelf.chat_room_connect_status = 0;
        }
    }];
}

- (void)exitChatroom:(WTChatRoomManagerBlock)completion{
    
    [self sendExitChatRoomMessage];
    [[NIMSDK sharedSDK].chatroomManager exitChatroom:self.chatRoomSession.sessionId completion:^(NSError * _Nullable error) {
        if(!error){
            completion(NO,1);
        }else {
            completion(NO,0);
        }
    }];
}

- (void)fetchChatRoomPersonCount:(NSString *)roomID completion:(void (^)(NSInteger))completion{
    
    [[NIMSDK sharedSDK].chatroomManager fetchChatroomInfo:roomID completion:^(NSError * _Nullable error, NIMChatroom * _Nullable chatroom) {
        if(!error){
            completion(chatroom.onlineUserCount);
        }else {
            completion(0);
        }
    }];
}

- (NIMMessage*)sendChatRoomMessage:(NSString *)messageBody{
    
    if(self.chat_room_connect_status == 0){
        return nil;
    }
    NIMMessage *message = [[NIMMessage alloc] init];
    message.text        = messageBody;
    message.remoteExt = [WTMessageFactory chatRoomMessageRemoteExt];
    [[[NIMSDK sharedSDK] chatManager] sendMessage:message toSession:self.chatRoomSession error:nil];
    return message;
}

- (NIMMessage*)sendChatRoomDamuMessage:(NSString *)messageBody{
    
    if(self.chat_room_connect_status == 0){
        return nil;
    }
    NIMMessage *message = [[NIMMessage alloc] init];
    message.text        = messageBody;
    message.remoteExt = [WTMessageFactory chatRoomDanMuMessageRemoteExt];
    [[[NIMSDK sharedSDK] chatManager] sendMessage:message toSession:self.chatRoomSession error:nil];
    return message;
}

- (NIMMessage*)sendGift:(NSDictionary *)giftInfo{
    
    if(self.chat_room_connect_status == 0){
        return nil;
    }
    NIMMessage *message = [[NIMMessage alloc] init];
    message.remoteExt = [WTMessageFactory giftRemoteExt:giftInfo];
    [[[NIMSDK sharedSDK] chatManager] sendMessage:message toSession:self.chatRoomSession error:nil];
    return message;
}

- (NIMMessage *)sendAnchorLeave{
    if(self.chat_room_connect_status == 0){
        return nil;
    }
    NIMMessage *message = [[NIMMessage alloc] init];
    message.remoteExt = [WTMessageFactory ownerLeaveRemoteExt];
    [[[NIMSDK sharedSDK] chatManager] sendMessage:message toSession:self.chatRoomSession error:nil];
    return message;
}

- (NIMMessage *)sendAnchorEnter{
    if(self.chat_room_connect_status == 0){
        return nil;
    }
    NIMMessage *message = [[NIMMessage alloc] init];
    message.remoteExt = [WTMessageFactory ownerEnterRemoteExt];
    [[[NIMSDK sharedSDK] chatManager] sendMessage:message toSession:self.chatRoomSession error:nil];
    return message;
}

- (NIMMessage *)stopLive{
    if(self.chat_room_connect_status == 0){
        return nil;
    }
    NIMMessage *message = [[NIMMessage alloc] init];
    message.remoteExt = [WTMessageFactory exitLiveRemoteExt];
    [[[NIMSDK sharedSDK] chatManager] sendMessage:message toSession:self.chatRoomSession error:nil];
    return message;
}
@end
